package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/*
* 统计数据初始化监听器
*
* */
//@Component
public class StatsDataIntListener implements ApplicationListener<ContextRefreshedEvent>{

    @Value("${from}")
    private  boolean from;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
  //spring容器创建bean完成后马上调用
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //是否初始化
        if(from){
            //完成统计数据的初始化,就是把mysql中的热数据搬到redis中  flushdb
            //1.查询所有文章对象
            List <StrategyDetail> list=strategyDetailService.list();
            //迭代没个对象取出热数据
            if(list.size()>0){
                for (StrategyDetail detail : list) {
                    Long detailId = detail.getId();
                    //redis中没有改统计对象的可以,才存储
                    if(!strategyStatsCacheService.hasStasKey(detailId)){
                        //3 封装成VO对象
                        StrategyStats vo = new StrategyStats(detailId,detail.getViewnum(),detail.getReplynum(),
                                detail.getFavornum(),detail.getSharenum(),detail.getThumbsupnum());

                        Destination dest = detail.getDest();
                        vo.setDestId(dest.getId());
                        vo.setDestName(dest.getName());
                        vo.setTitle(detail.getTitle());
                        vo.setStrategyId(detailId);
                        //4保存到redis 中
                        strategyStatsCacheService.setStrategyStats(vo);
                        //飙升排行版的数据初始化
                        if(!strategyStatsCacheService.exist(RedisKey.STRATEGY_STATS_UP_SORT,detail.getId())){
                            int num = detail.getViewnum() + detail.getReplynum();
                            strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_UP_SORT,num,detail.getId());
                        }

                        //热门排行版的数据初始化
                        if(!strategyStatsCacheService.exist(RedisKey.STRATEGY_STATS_HOT_SORT,detail.getId())){
                            int num = detail.getThumbsupnum() + detail.getFavornum();
                            strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_HOT_SORT,num,detail.getId());
                        }
                    }
                }
            }
        }
    }
}
